#include "ScreenShotControlWidget.h"


#include <QMenu>
#include <QMenuBar>
#include <QPoint>
#include <QColor>
#include <QIcon>
#include <QDir>
#include <QPainter>
#include <QPen>
#include <QFrame>
#include <QButtonGroup>
ScreenShotControlWidget::ScreenShotControlWidget(QWidget *parent)
    : QWidget{parent}
{

    this->setWindowFlags(Qt::Tool|Qt::FramelessWindowHint);
    this->setFixedSize(288,46);
    this->setContentsMargins(0,0,0,0);
    this->setStyleSheet("background-color:rgba(255,255,255,255);QPushButton { border: none; background: transparent;}");//去除所有按钮的边框
    //初始化颜色和画笔大小
    m_curPen=QPen(QColor(0,0,0),1);

    //初始化这些按钮
    m_controlLayout=new QHBoxLayout(this);
    m_DrawFuctionGroup=new QButtonGroup(this);

    m_verifyBtn=new QPushButton(this);
    m_closeBtn=new QPushButton(this);
    m_backBtn=new QPushButton(this);//back.svg
    m_addText=new QPushButton(this);//text.svg
    m_addArrow=new QPushButton(this);//arrow.svg
    m_addEllipse=new QPushButton(this);//ellipse.svg
    m_addRect=new QPushButton(this);//rectangle.svg
    m_addDrawCasual=new QToolButton(this);
    m_addDrawCasual->setText("展开选择项");//pen.svg
    //随意绘图按钮，可选择颜色和笔刷大小
    m_addDrawCasual->setCheckable(true);//可展开


    LoadBtnsIcon();
    InitLayout();
    BindSignalandSlot();



}

void ScreenShotControlWidget::InitLayout()
{
    this->setLayout(m_controlLayout);

    //将绘画功能按钮添加进一个按钮组
    m_DrawFuctionGroup->addButton(m_addRect);
    m_DrawFuctionGroup->addButton(m_addEllipse);
    m_DrawFuctionGroup->addButton(m_addArrow);
    // m_DrawFuctionGroup->addButton(m_addDrawCasual);
    m_DrawFuctionGroup->addButton(m_addText);

    m_DrawFuctionGroup->setExclusive(true);//设置只能同时选择一个

    //其他按钮添加进布局
    m_controlLayout->addWidget(m_addRect);
    m_controlLayout->addWidget(m_addEllipse);
    m_controlLayout->addWidget(m_addArrow);
    m_controlLayout->addWidget(m_addDrawCasual);
    m_controlLayout->addWidget(m_addText);
    m_controlLayout->addWidget(m_backBtn);
    m_controlLayout->addWidget(m_closeBtn);
    m_controlLayout->addWidget(m_verifyBtn);



    ///以下编写这个可展开按钮的布局
    /// 1.一个widget容器
    /// 2.两组btns,分别需要两个groupbtn,里边分别塞几个按钮，按钮+之间的关系是不可同时选中
    /// 3。两组按钮之间有一条竖线，推荐使用QFrame实现
    m_casualDrawWidgetContainer=new QWidget(this);
    m_casualDrawWidgetContainer->setWindowFlags(Qt::Tool|Qt::FramelessWindowHint);
    m_casualDrawWidgetContainer->setContentsMargins(0,0,0,0);
    m_casualDrawWidgetContainer->setStyleSheet("background-color:rgba(255,255,255,255);");


    //管理笔刷大小按钮组
    QButtonGroup * btngroups_pensize=new QButtonGroup(this);
    btngroups_pensize->setExclusive(true);//设置按钮之间互斥
    QHBoxLayout *btnsgrouplayout=new QHBoxLayout(m_casualDrawWidgetContainer);
    btnsgrouplayout->setAlignment(Qt::AlignLeft);

    //三个笔刷
    QPushButton *btn_1=new QPushButton(m_casualDrawWidgetContainer);
    QPushButton *btn_2=new QPushButton(m_casualDrawWidgetContainer);
    QPushButton *btn_3=new QPushButton(m_casualDrawWidgetContainer);

    btngroups_pensize->addButton(btn_1,1);
    btngroups_pensize->addButton(btn_2,2);
    btngroups_pensize->addButton(btn_3,3);

    btnsgrouplayout->addWidget(btn_1);
    btnsgrouplayout->addWidget(btn_2);
    btnsgrouplayout->addWidget(btn_3);

    btn_1->setIcon(QIcon(iconpath+"circle_16.svg"));
    btn_1->setIconSize(QSize(8,8));

    btn_2->setIcon(QIcon(iconpath+"circle_32.svg"));
    btn_2->setIconSize(QSize(10,10));

    btn_3->setIcon(QIcon(iconpath+"circle_48.svg"));
    btn_3->setIconSize(QSize(12,12));


    //绑带那个这三个按钮对应的信号槽函数
    connect(btn_1,&QPushButton::clicked,this,[&](){
        m_curPen.setWidth(1);
        emit ChangePen();
    });

    connect(btn_2,&QPushButton::clicked,this,[&](){
        m_curPen.setWidth(3);
        emit ChangePen();
    });

    connect(btn_3,&QPushButton::clicked,this,[&](){
        m_curPen.setWidth(4);
        emit ChangePen();
    });


    //竖线
    QFrame *seperator=new QFrame();
    seperator->setFrameShape(QFrame::VLine);
    // seperator->setFrameShadow(QFrame::Raised);

    btnsgrouplayout->addWidget(seperator);

    //右侧按钮组-管理颜色按钮组
    QButtonGroup *btngroup_color=new QButtonGroup(this);
    btngroup_color->setExclusive(true);//设置同时只能选中一个

    QPushButton *btn_red=new QPushButton(m_casualDrawWidgetContainer);
    QPushButton *btn_blue=new QPushButton(m_casualDrawWidgetContainer);
    QPushButton *btn_green=new QPushButton(m_casualDrawWidgetContainer);
    QPushButton *btn_yellow=new QPushButton(m_casualDrawWidgetContainer);
    QPushButton *btn_black=new QPushButton(m_casualDrawWidgetContainer);
    QPushButton *btn_gray=new QPushButton(m_casualDrawWidgetContainer);

    btngroup_color->addButton(btn_red,1);
    btngroup_color->addButton(btn_blue,2);
    btngroup_color->addButton(btn_green,3);
    btngroup_color->addButton(btn_yellow,4);
    btngroup_color->addButton(btn_black,5);
    btngroup_color->addButton(btn_gray,6);

    btnsgrouplayout->addWidget(btn_red);
    btnsgrouplayout->addWidget(btn_blue);
    btnsgrouplayout->addWidget(btn_green);
    btnsgrouplayout->addWidget(btn_yellow);
    btnsgrouplayout->addWidget(btn_black);
    btnsgrouplayout->addWidget(btn_gray);

    btn_red->setIcon(QIcon(iconpath+"red.svg"));
    btn_blue->setIcon(QIcon(iconpath+"blue.svg"));
    btn_green->setIcon(QIcon(iconpath+"green.svg"));
    btn_yellow->setIcon(QIcon(iconpath+"yellow.svg"));
    btn_black->setIcon(QIcon(iconpath+"black.svg"));
    btn_gray->setIcon(QIcon(iconpath+"gray.svg"));


    //绑带那个颜色按钮对应的信号槽函数
    connect(btn_red,&QPushButton::clicked,this,[&](){
        m_curPen.setColor(QColor(255,0,0));
        emit ChangePen();
    });

    connect(btn_blue,&QPushButton::clicked,this,[&](){
        m_curPen.setColor(QColor(0,0,255));
        emit ChangePen();
    });

    connect(btn_green,&QPushButton::clicked,this,[&](){
        m_curPen.setColor(QColor(0,255,0));
        emit ChangePen();
    });
    connect(btn_yellow,&QPushButton::clicked,this,[&](){
        m_curPen.setColor(QColor(255,255,100));
        emit ChangePen();
    });

    connect(btn_gray,&QPushButton::clicked,this,[&](){
        m_curPen.setColor(QColor(0,0,0,100));
        emit ChangePen();
    });

    connect(btn_black,&QPushButton::clicked,this,[&](){
        m_curPen.setColor(QColor(0,0,0));
        emit ChangePen();
    });

    m_casualDrawWidgetContainer->adjustSize();
}



void ScreenShotControlWidget::BindSignalandSlot()
{
    //确认按钮信号槽
    connect(m_verifyBtn,&QPushButton::clicked,this,&ScreenShotControlWidget::OnVerifyBtnClicked);

    //关闭按钮信号槽
    connect(m_closeBtn,&QPushButton::clicked,this,&ScreenShotControlWidget::OnCloseBtnClicked);

    //回退按钮信号槽
    connect(m_backBtn,&QPushButton::clicked,this,&ScreenShotControlWidget::OnBackBtnClicked);

    //添加文本按钮信号槽
    connect(m_addText,&QPushButton::clicked,this,&ScreenShotControlWidget::OnDrawTextBtnClicked);

    //添加箭头按钮信号槽
    connect(m_addArrow,&QPushButton::clicked,this,&ScreenShotControlWidget::OnDrawArrowBtnClicked);

    //添加椭圆按钮信号槽
    connect(m_addEllipse,&QPushButton::clicked,this,&ScreenShotControlWidget::OnDrawEllipseBtnClicked);

    //添加矩形按钮信号槽
    connect(m_addRect,&QPushButton::clicked,this,&ScreenShotControlWidget::OnDrawRectBtnClicked);

    connect(m_addDrawCasual,&QToolButton::toggled,this,[this](bool checked){
        this->OnDrawCasualBtnClicked(checked);
        emit DrawCasual();
    });

}

void ScreenShotControlWidget::LoadBtnsIcon()
{
    //加载资源文件作为图标
    QDir icondir=QDir::currentPath() ;

    iconpath=icondir.absolutePath()+"/res/icon/";

    m_verifyBtn->setIcon(QIcon(iconpath+"verify.svg"));
    m_verifyBtn->setIconSize(QSize(18,18));
    m_closeBtn->setIcon(QIcon(iconpath+"close.svg"));
    m_backBtn->setIcon(QIcon(iconpath+"back.svg"));
    m_addText->setIcon(QIcon(iconpath+"text.svg"));
    m_addArrow->setIcon(QIcon(iconpath+"arrow.svg"));
    m_addDrawCasual->setIcon(QIcon(iconpath+"pen.svg"));
    m_addRect->setIcon(QIcon(iconpath+"rectangle.svg"));
    m_addEllipse->setIcon(QIcon(iconpath+"ellipse.svg"));
}

QPen ScreenShotControlWidget::GetPen() const
{
    return m_curPen;
}

void ScreenShotControlWidget::OnVerifyBtnClicked()
{
    emit  SaveCapture();
}

void ScreenShotControlWidget::OnCloseBtnClicked()
{
    //关闭当前窗口和负数的工具窗口
    m_casualDrawWidgetContainer->close();
    this->close();
    emit CloseCapture();
}

void ScreenShotControlWidget::OnDrawRectBtnClicked()
{
    emit DrawRect();
}

void ScreenShotControlWidget::OnDrawEllipseBtnClicked()
{
    emit DrawEllipse();
}

void ScreenShotControlWidget::OnDrawArrowBtnClicked()
{
    emit DrawArrow();
}

void ScreenShotControlWidget::OnDrawTextBtnClicked()
{
    emit DrawText();
}

void ScreenShotControlWidget::OnDrawCasualBtnClicked(bool checked)
{
    //下边这个窗口设置为可以点击收起
    m_casualDrawWidgetContainer->setVisible(checked);

    qreal scalefactor=this->screen()->devicePixelRatio();
    //当前窗口的相对位置
    QRect rect=this->geometry();
    //控制窗口右边界
    QPoint widget_bottonright=this->mapToGlobal(this->rect().bottomRight());//这个相对于父对象的位置

    //以下看看maptoparent和maptoglobal的区别

    QPoint sub_widgetpoint;
    int x=(widget_bottonright.x()-m_casualDrawWidgetContainer->width());
    int y=widget_bottonright.y()+3;
    sub_widgetpoint=QPoint(x,y);
    m_casualDrawWidgetContainer->move(x,y);
    m_casualDrawWidgetContainer->adjustSize();
}

void ScreenShotControlWidget::OnBackBtnClicked()
{
    emit ComeBack();
}
